﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Added
using System.IO;
using System.Xml;

namespace GELib
{
    public static class GEProject
    {
        private static Boolean isProjectOpened;
        private static String myPath;
        private static String myTitle;
        private static String myVersion;
        private static String myAuthors;


        public static event EventHandler projectOpened;
        public static event EventHandler projectClosed;
        public static event EventHandler projectModified;


        #region "properties"
        public static Boolean aProjectIsOpened { get { return isProjectOpened; } }
        public static String Path { get { return myPath; } }
        public static String Title
        {
            get { return myTitle; }
            set
            {
                myTitle = value;
                if (projectModified != null) projectModified(null, EventArgs.Empty);
            }
        }
        public static String Version
        {
            get { return myVersion; }
            set
            {
                myVersion = value;
                if (projectModified != null) projectModified(null, EventArgs.Empty);
            }
        }
        public static String Authors
        {
            get { return myAuthors; }
            set
            {
                myAuthors = value;
                if (projectModified != null) projectModified(null, EventArgs.Empty);
            }
        }
        #endregion


        public static void createProject(String projectPath, String projectTitle, String projectAuthors)
        {
            // Create project file (xml)
            XmlDocument configFile = new XmlDocument();
            XmlDeclaration doctype = configFile.CreateXmlDeclaration("1.0", "utf-8", null);
            configFile.InsertBefore(doctype, configFile.DocumentElement);

            XmlElement rootNode = configFile.CreateElement("projectConfig");
            configFile.InsertAfter(rootNode, doctype);

            // insert title
            XmlElement tmpNode = configFile.CreateElement("projectTitle");
            tmpNode.InnerText = projectTitle;
            rootNode.InsertAfter(tmpNode, rootNode.LastChild);

            // insert version
            tmpNode = configFile.CreateElement("projectVersion");
            tmpNode.InnerText = "0.01";
            rootNode.InsertAfter(tmpNode, rootNode.LastChild);

            // insert authors
            tmpNode = configFile.CreateElement("projectAuthors");
            tmpNode.InnerText = projectAuthors;
            rootNode.InsertAfter(tmpNode, rootNode.LastChild);

            configFile.Save(projectPath + "\\" + projectTitle + ".GEPj");

            // Create folders
            Directory.CreateDirectory(projectPath + "\\Resources");
            Directory.CreateDirectory(projectPath + "\\Maps");
        }

        public static void openProject(String projectFilePath)
        {
            if (myPath != "")
                closeProject();

            // Load project info from projectPath
            FileInfo fi = new FileInfo(projectFilePath);
            myPath = fi.Directory.FullName;

            try
            {
                XmlDocument configFile = new XmlDocument();
                configFile.Load(projectFilePath);
                XmlNode rootNode = configFile.LastChild;

                // get infos from rootNode
                myTitle = searchXmlValue(rootNode, "projectTitle");
                myVersion = searchXmlValue(rootNode, "projectVersion");
                myAuthors = searchXmlValue(rootNode, "projectAuthors");


                isProjectOpened = true;

                // fire the projectOpened event
                if (projectOpened != null)
                    projectOpened(null, EventArgs.Empty);
            }
            catch { }
        }

        public static void closeProject()
        {
            if (myPath != "") // a project is opened
            {
                // clear all variables
                myPath = "";
                myTitle = "";
                myVersion = "";
                myAuthors = "";


                isProjectOpened = false;

                if (projectClosed != null)
                    projectClosed(null, EventArgs.Empty);
            }
        }

        private static String searchXmlValue(XmlNode rootNode, String fieldName)
        {
            String toReturn = "";

            Int32 i = 0;
            while ((toReturn == "") & (i <= rootNode.ChildNodes.Count))
            {
                if (rootNode.ChildNodes[i].Name == fieldName)
                    toReturn = rootNode.ChildNodes[i].InnerText;

                i++;
            }

            return toReturn;
        }

    }

}

/**
 *        Project.GEPj file format
 *        
 * <doctype>
 * <projectConfig>
 * 
 * <projectTitle></projectTitle>
 * <projectVersion></projectVersion>
 * <projectAuthors></projectAuthors>
 * 
 * </projectConfig>
 * */
